import { _decorator, Component, Label, Node, tween, Tween, UIOpacity, v3, Vec3 } from 'cc';
import { PoolManager } from '../Managers/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('DamageText')
export class DamageText extends Component {
    uiOpacity: UIOpacity = null!;
    label: Label = null!;

    onLoad() {
        this.uiOpacity = this.node.getComponent(UIOpacity);
        this.label = this.node.getChildByName(`Label`).getComponent(Label);
    }

    Show(content: string, delay: number = 0.35, type: number = 1) {
        this.uiOpacity.opacity = 255;
        this.node.setScale(Vec3.ONE);

        this.label.string = `${content}`;
        this.label.updateRenderData(true);

        Tween.stopAllByTarget(this.node);
        Tween.stopAllByTarget(this.uiOpacity);

        switch (type) {
            case 1:
                this.ShowTween1(delay);
                break;
            case 2:
                this.ShowTween2(delay);
                break;
            default:
                this.ShowTween1(delay);
                break;
        }
    }

    ShowTween1(delay: number) {
        this.node.setScale(Vec3.ZERO);

        tween(this.node)
            .to(0.2, { scale: v3(1, 1) }, { easing: `backOut` })
            .delay(delay)
            .call(() => tween(this.uiOpacity).to(0.25, { opacity: 100 }).start())
            .to(0.25, { position: v3(this.node.position.x, this.node.position.y + 180) })
            .call(() => PoolManager.PutNode(this.node))
            .start();
    }

    ShowTween2(delay: number) {
        tween(this.uiOpacity).delay(delay).to(0.25, { opacity: 0 }).start();
        tween(this.node).delay(delay).to(0.25, { position: v3(0, 250) }).call(() => { PoolManager.PutNode(this.node); }).start();
    }

}
